<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>作业管理系统 - 登录</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css">
    <script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <style>
        .login-wrapper {
            width: 100%;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background: #f2f2f2;
            background-image: linear-gradient(to bottom right, #2c3e50, #3498db);
        }
        .login-box {
            width: 400px;
            padding: 40px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
    </style>
</head>
<body>
    <div class="login-wrapper">
        <div class="login-box">
            <h2 class="login-title">作业管理系统</h2>
            <form class="layui-form" action="${pageContext.request.contextPath}/login" method="post">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="userType" value="student" title="学生" checked>
                        <input type="radio" name="userType" value="teacher" title="教师">
                        <input type="radio" name="userType" value="admin" title="管理员">
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" required lay-verify="required" 
                               placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" required lay-verify="required" 
                               placeholder="请输入密码" class="layui-input">
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="loginForm">
                            <i class="layui-icon layui-icon-ok"></i> 登录
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script>
        layui.use(['form', 'layer'], function(){
            var form = layui.form;
            var layer = layui.layer;
            
            // 添加表单验证规则
            form.verify({
                username: function(value, item) {
                    var userType = $('input[name="userType"]:checked').val();
                    if (userType === 'student' || userType === 'teacher') {
                        if (!/^\d+$/.test(value)) {
                            return '请输入正确的学号/教师号';
                        }
                    }
                }
            });
            
            form.on('submit(loginForm)', function(data){
                var loadIndex = layer.load(2);
                
                $.post('${pageContext.request.contextPath}/login', data.field, function(res){
                    layer.close(loadIndex);
                    if(res.code === 0){
                        layer.msg('登录成功', {icon: 1}, function(){
                            switch(data.field.userType){
                                case 'student':
                                    window.location.href = '${pageContext.request.contextPath}/student/assignment/list';
                                    break;
                                case 'teacher':
                                    window.location.href = '${pageContext.request.contextPath}/teacher/assignment/list';
                                    break;
                                case 'admin':
                                    window.location.href = '${pageContext.request.contextPath}/admin/students';
                                    break;
                            }
                        });
                    } else {
                        layer.msg(res.msg || '登录失败', {icon: 2});
                    }
                }).fail(function(xhr) {
                    layer.close(loadIndex);
                    layer.msg('服务器错误，请稍后重试', {icon: 2});
                });
                return false;
            });
        });
    </script>
</body>
</html> 